//
//  AppDelegate.m
//  cityCar
//
//  Created by yao on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "InformationViewController.h"
#import "ForumViewController.h"
#import "SeekCarViewController.h"
#import "MineViewController.h"
#import "SearchViewController.h"
#import "SettingViewController.h"
#import "WebColor.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"

@implementation AppDelegate
- (void)dealloc
{
    [_view release];
    [_window release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
  
    self.view = [[UIView alloc]initWithFrame:self.window.bounds];
    _view.backgroundColor = [WebColor black];
    _view.userInteractionEnabled = NO;
    _view.alpha = 0.5;
//    [_window addSubview:_view];
//    [self.window sendSubviewToBack:_view];
    [_view release];

    
    [_window release];
    
    
    // 分享
    [ShareSDK registerApp:@"api20"];
    
    [ShareSDK connectSinaWeiboWithAppKey:@"1058363997" appSecret:@"38051da90358cda980eda863510dde5e" redirectUri:@"http://blog.csdn.net/jelly_1992"];
    
    [ShareSDK connectTencentWeiboWithAppKey:@"" appSecret:@"" redirectUri:@"http://www.sharesdk.cn" wbApiCls:[WeiboApi class]];
    
    
    // 开机动画
    UIView *headView = [[UIView alloc] initWithFrame:self.window.frame];
    headView.backgroundColor = [WebColor whiteSmoke];
    headView.tag = 19919;
    
    
    [self.window addSubview:headView];
    [self.window bringSubviewToFront:headView];
    [headView release];
    
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:headView.frame];
    myImageView.image = [UIImage imageNamed:@"comeOn.png"];
    myImageView.contentMode = UIViewContentModeScaleAspectFill;
    [headView addSubview:myImageView];
    [myImageView release];

    
    [UIView animateWithDuration:3 animations:^{
        myImageView.frame = CGRectMake(- 25, - 25, headView.frame.size.width + 50, headView.frame.size.height + 50);
        myImageView.alpha = 0.4;
    } completion:^(BOOL finished) {
        [self cartoon];
    }];
    
    

    
    return YES;
}

- (void)cartoon
{
    // 判断网络连接
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    Reachability *hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];//可以以多种形式初始化
    [hostReach startNotifier];  //开始监听,会启动一个run loop

    
    UIView *headView = [self.window viewWithTag:19919];
    
    [headView removeFromSuperview];
    // 资讯页
    InformationViewController *informationVC = [[InformationViewController alloc] init];
    UINavigationController *informationNav = [[UINavigationController alloc]initWithRootViewController:informationVC];
    informationNav.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"资讯" image:[UIImage imageNamed:@"infor16.png"] selectedImage:[UIImage imageNamed:@"infor32.png"]] autorelease];
    
    // 论坛页
    ForumViewController *forumVC = [[ForumViewController alloc] init];
    UINavigationController *forumNav = [[UINavigationController alloc]initWithRootViewController:forumVC];
    forumNav.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"论坛" image:[UIImage imageNamed:@"forum16.png"] selectedImage:[UIImage imageNamed:@"forum32.png"]] autorelease];
    
    
    // 找车页
    SeekCarViewController *seekCarVC = [[SeekCarViewController alloc] init];
    UINavigationController *seekCarNav = [[UINavigationController alloc]initWithRootViewController:seekCarVC];
    seekCarNav.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"找车" image:[UIImage imageNamed:@"seekCar16.png"] selectedImage:[UIImage imageNamed:@"seekCar32.png"]] autorelease];
    
    
    // 个人中心页
    MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *mineNav = [[UINavigationController alloc]initWithRootViewController:mineVC];
    mineNav.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"个人中心" image:[UIImage imageNamed:@"mine16.png"] selectedImage:[UIImage imageNamed:@"mine32.png"]] autorelease];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.tabBar.tintColor = [WebColor darkGray];
    tabBar.viewControllers = @[informationNav, forumNav, seekCarNav, mineNav];
    
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    self.sideViewController = [[YRSideViewController alloc] init];
    self.sideViewController.rootViewController = tabBar;
    self.sideViewController.leftViewController = searchVC;
    self.sideViewController.rightViewController = settingVC;
    self.sideViewController.leftViewShowWidth = 270;
    self.sideViewController.needSwipeShowMenu = true;
    
    self.window.rootViewController = self.sideViewController;
    
    [_sideViewController release];
    [searchVC release];
    [settingVC release];
    [tabBar release];
    [informationVC release];
    [informationNav release];
    [forumVC release];
    [forumNav release];
    [seekCarVC release];
    [seekCarNav release];
    [mineVC release];
    [mineNav release];
    
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self selector:@selector(notiCenterAction:) name:@"theNight" object:nil];
}

// 接收夜间模式的通知
- (void)notiCenterAction:(NSNotification *)noti
{
    // 收到通知之后要执行的方法
    NSLog(@"夜间模式");
    
    NSLog(@"%@ == %@", noti.object, noti.userInfo);
    [self.window bringSubviewToFront:_view];
    if ([noti.object isEqualToString:@"on"]) {
        [self.window addSubview:_view];
//        [self.window bringSubviewToFront:_view];
    } else if ([noti.object isEqualToString:@"off"])
    {
        [self.view removeFromSuperview];
//        [self.window sendSubviewToBack:_view];
    }
    
    
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    switch (tabBarController.selectedIndex) {
//        case 0:
//            tabBarController.title = @"都市汽车";
//            break;
//        case 1:
//            tabBarController.title = @"论坛";
//            break;
//        case 2:
//            tabBarController.title = @"找车";
//            break;
//        case 3:
//            tabBarController.title = @"购车优惠";
//            break;
//        case 4:
//            tabBarController.title = @"个人中心";
//            break;
//        default:
//            break;
//    }
//}
#pragma mark 判断网络状态
// 连接改变
- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}

//处理连接改变后的情况
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    //对连接改变做出响应的处理动作。
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 110, 110)];
    label.center = self.window.center;
    label.alpha = 0.5;
    label.numberOfLines = 0;
    label.font = [UIFont boldSystemFontOfSize:17.f];
    label.layer.cornerRadius = 10;
    label.clipsToBounds = YES;
    label.backgroundColor = [WebColor black];
    label.textColor = [WebColor white];
//    [self.window addSubview:label];
//    [self.window sendSubviewToBack:label];
    [label release];
    
    if (status == ReachableViaWWAN) {  //3G网络就弹出提实况
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"数据流量连接"
                                                        message:@"建议WiFi下使用，土豪随意"
                                                       delegate:nil
                                              cancelButtonTitle:@"YES" otherButtonTitles:nil];
        [alert show];
        [alert release];
    } else if (status == NotReachable) {  //没有连接到网络就弹出提实况
        
        label.text = @"似乎已断开与互联网的连接";
        [self.window addSubview:label];
        [UIView animateWithDuration:3 animations:^{
            label.alpha = 0.1;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
